package com.junmiao.A.cron;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;
import java.util.TreeMap;

import com.junmiao.A.dataSource.DataSource;
import com.junmiao.A.execution.Execution;
import com.junmiao.A.stockModel.ShareHolder;
import com.junmiao.A.stockModel.SinaGGTJBean;
import com.junmiao.A.stockModel.SinaJSBean;
import com.junmiao.A.stockModel.SinaStockBean;
import com.junmiao.A.stockModel.SohuStockBean;

public class MainCron {

	public static void main(String args[]) {

		try {
			//printRawGGTJ();
			
			//printRawSinaJS();
			
			//printRawSohuStock();
			
			//printRawSinaStock();
			
			Map<String,String> map = printZiJinLiuru();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	

	public static void printRawGGTJ(DataSource ds) throws IOException {

		Calendar cal = Calendar.getInstance();

		String dirName = "" + cal.get(Calendar.YEAR)
				+ (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);

		File f = new File("c:\\junmiaostock\\" + dirName);

		if (!f.exists()) {
			f.mkdir();
		}

		File f1 = new File("c:\\junmiaostock\\" + dirName + "\\raw");

		if (!f1.exists()) {
			f1.mkdir();
		}

		File f3 = new File("c:\\junmiaostock\\" + dirName + "\\raw"
				+ "\\ggtj.txt");

		FileWriter fw = new FileWriter(f3);
		BufferedWriter bw = new BufferedWriter(fw);

		ArrayList<SinaGGTJBean> ttgj = Execution.executeSinaGGTJ(ds);

		if (ttgj != null && ttgj.size() > 0) {
			for (int i = 0; i < ttgj.size(); i++) {
				SinaGGTJBean ggtj = ttgj.get(i);

				if (ggtj != null) {
					String line = ggtj.getStockNo() + ":" + ggtj.getStockName()
							+ ":" + ggtj.getStock_org_hold() + ":"
							+ ggtj.getOrg_hold_today() + ":"
							+ ggtj.getOrg_hold_5() + ":"
							+ ggtj.getIndiv_hold_today() + ":"
							+ ggtj.getIndiv_hold_5();

					bw.write(line + "\n");
				}
			}
		}

		bw.close();
		fw.close();
	}
	
	public static void printRawSinaJS(DataSource ds) throws IOException {

		Calendar cal = Calendar.getInstance();

		String dirName = "" + cal.get(Calendar.YEAR)
				+ (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);

		File f = new File("c:\\junmiaostock\\" + dirName);

		if (!f.exists()) {
			f.mkdir();
		}

		File f1 = new File("c:\\junmiaostock\\" + dirName + "\\raw");

		if (!f1.exists()) {
			f1.mkdir();
		}

		File f3 = new File("c:\\junmiaostock\\" + dirName + "\\raw"
				+ "\\sinajs.txt");

		FileWriter fw = new FileWriter(f3);
		BufferedWriter bw = new BufferedWriter(fw);

		ArrayList<SinaJSBean> sjsb = Execution.executeSinaJS(ds);

		if (sjsb != null && sjsb.size() > 0) {
			for (int i = 0; i < sjsb.size(); i++) {
				SinaJSBean jsb = sjsb.get(i);

				if (jsb != null) {
					String line = jsb.getStockNo() + ":" + jsb.getStockName()
							+ ":" + jsb.getToday_close()
							+ ":" + jsb.getLastday_close()
							+ ":" + jsb.getToday_high()
							+ ":" + jsb.getToday_low()
							+ ":" + jsb.getToday_open();
					
					try{
						
						BigDecimal open = new BigDecimal(jsb.getLastday_close());
						BigDecimal close = new BigDecimal(jsb.getToday_close());
						
						BigDecimal diff = close.divide(open,4,BigDecimal.ROUND_HALF_UP).subtract(new BigDecimal("1")).multiply(new BigDecimal("100"));
						
						line += ":" + diff.toString().substring(0,diff.toString().length()-2);
						
					}catch(RuntimeException e)
					{
						line += ":0.00";
					}
				
					

					bw.write(line + "\n");
				}
			}
		}

		bw.close();
		fw.close();
	}
	
	public static void printRawSohuStock(DataSource ds) throws IOException {

		Calendar cal = Calendar.getInstance();

		String dirName = "" + cal.get(Calendar.YEAR)
				+ (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);

		File f = new File("c:\\junmiaostock\\" + dirName);

		if (!f.exists()) {
			f.mkdir();
		}

		File f1 = new File("c:\\junmiaostock\\" + dirName + "\\raw");

		if (!f1.exists()) {
			f1.mkdir();
		}

		File f3 = new File("c:\\junmiaostock\\" + dirName + "\\raw"
				+ "\\shst.txt");

		FileWriter fw = new FileWriter(f3);
		BufferedWriter bw = new BufferedWriter(fw);

		ArrayList<SohuStockBean> ttgj = Execution.executeSohuStock(ds);

		if (ttgj != null && ttgj.size() > 0) {
			for (int i = 0; i < ttgj.size(); i++) {
				SohuStockBean ggtj = ttgj.get(i);

				if (ggtj != null) {
					String line = ggtj.getStockNo() + ":" + ggtj.getStockName()
							+ ":" + ggtj.getBiao_xian_10_day() + ":"
							+ ggtj.getBiao_xian_5_day() + ":"
							+ ggtj.getHuan_shou_10_day() + ":"
							+ ggtj.getHuan_shou_5_day() + ":"
							+ ggtj.getJin_li_zen_zhang() + ":"
							+ ggtj.getJin_zi_zhan_shou_yi_lv() + ":"
							+ ggtj.getZhu_yin_zen_zhan_lv() + ":"
							+ ggtj.getLiu_tong_gu_ben() + ":"
							+ ggtj.getZong_gu_bin() + ":" 
							+ ggtj.getMei_gu_jin_zi_chan() + ":"
							+ ggtj.getMei_gu_shou_yi() + ":"
							+ ggtj.getMei_gu_wei_fen_pei() + ":"
							+ ggtj.getMei_gu_zi_ben_gong_ji_jin()+":"
							+ ggtj.getYearHigh()+ ":"
					        + ggtj.getYearLow();
					

					bw.write(line + "\n");
				}
			}
		}

		bw.close();
		fw.close();
	}
	
	public static void printRawSinaStock(DataSource ds) throws IOException {

		Calendar cal = Calendar.getInstance();

		String dirName = "" + cal.get(Calendar.YEAR)
				+ (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);

		File f = new File("c:\\junmiaostock\\" + dirName);

		if (!f.exists()) {
			f.mkdir();
		}

		File f1 = new File("c:\\junmiaostock\\" + dirName + "\\raw");

		if (!f1.exists()) {
			f1.mkdir();
		}

		File f3 = new File("c:\\junmiaostock\\" + dirName + "\\raw"
				+ "\\sinaStock.txt");

		FileWriter fw = new FileWriter(f3);
		BufferedWriter bw = new BufferedWriter(fw);

		ArrayList<SinaStockBean> ttgj = Execution.executeSinaStock(ds);

		if (ttgj != null && ttgj.size() > 0) {
			for (int i = 0; i < ttgj.size(); i++) {
				SinaStockBean ggtj = ttgj.get(i);

				if (ggtj != null) {
					String line = ggtj.getStockNo() + ":" + ggtj.getStockName();
					
					if(ggtj.getShareHolders()!=null&&ggtj.getShareHolders().size()==10)
					{
						for(int k=0;k<10;k++)
						{
							ShareHolder sh = ggtj.getShareHolders().get(k);
							if(sh!=null)
							{
								line += ":" + sh.getName()
									   + ":" + sh.getHolderAmount()
									   + ":" + sh.getOperation()
									   + ":" + sh.getDelta();
							}
						}
					}
					

					bw.write(line + "\n");
				}
			}
		}

		bw.close();
		fw.close();
	}
	
	
public static Map<String,String> printZiJinLiuru() throws IOException{
		
		Calendar cal = Calendar.getInstance();

		String dirName = "" + cal.get(Calendar.YEAR)
				+ (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);

	
		File f2 = new File("c:\\junmiaostock\\" + dirName + "\\raw\\ggtj.txt");
		File f1 = new File("c:\\junmiaostock\\" + dirName + "\\zjlr_jg.txt");
		File f3 = new File("c:\\junmiaostock\\" + dirName + "\\zjlr_sh.txt");
		
		BufferedReader br = new BufferedReader(new FileReader(f2));
        BufferedWriter bw = new BufferedWriter(new FileWriter(f1));
        BufferedWriter bw1 = new BufferedWriter(new FileWriter(f3));
        
        Map<String,String> map1 = new TreeMap<String, String>();
        Map<String,String> map2 = new TreeMap<String, String>();
        
		String s = "";
		while((s = br.readLine())!=null)
		{
			String[] stats = s.split(":");
			
			if(stats!=null&&stats.length==7)
			{
				if((int)(stats[5].charAt(0)) == 8593 && (int)(stats[6].charAt(0)) == 8593)
				{
					//bw.write(s + "\n");
					map1.put(stats[0], s+"\n");
					if((int)(stats[3]).charAt(0) == 8593 && stats[3].charAt(1)!='-'&&(int)(stats[4].charAt(0)) == 8593)
					{
						//bw1.write(s + "\n");
						map2.put(stats[0], s+"\n");
					}
				}
			}
		}
	    
		for(Map.Entry<String,String>entry: map1.entrySet())
		{ 	
			bw.write(entry.getValue());
		}
		
		for(Map.Entry<String,String>entry: map2.entrySet())
		{ 	
			bw1.write(entry.getValue());
		}
		
		
		
		br.close();
		bw1.close();
		bw.close();
		
		return map1;
	}

	public static void printRawGGTJ() throws IOException {

		Calendar cal = Calendar.getInstance();

		String dirName = "" + cal.get(Calendar.YEAR)
				+ (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);

		File f = new File("c:\\junmiaostock\\" + dirName);

		if (!f.exists()) {
			f.mkdir();
		}

		File f1 = new File("c:\\junmiaostock\\" + dirName + "\\raw");

		if (!f1.exists()) {
			f1.mkdir();
		}

		File f3 = new File("c:\\junmiaostock\\" + dirName + "\\raw"
				+ "\\ggtj.txt");

		FileWriter fw = new FileWriter(f3);
		BufferedWriter bw = new BufferedWriter(fw);

		ArrayList<SinaGGTJBean> ttgj = Execution.executeSinaGGTJ();

		if (ttgj != null && ttgj.size() > 0) {
			for (int i = 0; i < ttgj.size(); i++) {
				SinaGGTJBean ggtj = ttgj.get(i);

				if (ggtj != null) {
					String line = ggtj.getStockNo() + ":" + ggtj.getStockName()
							+ ":" + ggtj.getStock_org_hold() + ":"
							+ ggtj.getOrg_hold_today() + ":"
							+ ggtj.getOrg_hold_5() + ":"
							+ ggtj.getIndiv_hold_today() + ":"
							+ ggtj.getIndiv_hold_5();

					bw.write(line + "\n");
				}
			}
		}

		bw.close();
		fw.close();
	}
	
	public static void printRawSinaJS() throws IOException {

		Calendar cal = Calendar.getInstance();

		String dirName = "" + cal.get(Calendar.YEAR)
				+ (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);

		File f = new File("c:\\junmiaostock\\" + dirName);

		if (!f.exists()) {
			f.mkdir();
		}

		File f1 = new File("c:\\junmiaostock\\" + dirName + "\\raw");

		if (!f1.exists()) {
			f1.mkdir();
		}

		File f3 = new File("c:\\junmiaostock\\" + dirName + "\\raw"
				+ "\\sinajs.txt");

		FileWriter fw = new FileWriter(f3);
		BufferedWriter bw = new BufferedWriter(fw);

		ArrayList<SinaJSBean> sjsb = Execution.executeSinaJS();

		if (sjsb != null && sjsb.size() > 0) {
			for (int i = 0; i < sjsb.size(); i++) {
				SinaJSBean jsb = sjsb.get(i);

				if (jsb != null) {
					String line = jsb.getStockNo() + ":" + jsb.getStockName()
							+ ":" + jsb.getToday_close()
							+ ":" + jsb.getLastday_close()
							+ ":" + jsb.getToday_high()
							+ ":" + jsb.getToday_low()
							+ ":" + jsb.getToday_open();
					
					try{
						
						BigDecimal open = new BigDecimal(jsb.getLastday_close());
						BigDecimal close = new BigDecimal(jsb.getToday_close());
						
						BigDecimal diff = close.divide(open,4,BigDecimal.ROUND_HALF_UP).subtract(new BigDecimal("1")).multiply(new BigDecimal("100"));
						
						line += ":" + diff.toString().substring(0,diff.toString().length()-2);
						
					}catch(RuntimeException e)
					{
						line += ":0.00";
					}
				
					

					bw.write(line + "\n");
				}
			}
		}

		bw.close();
		fw.close();
	}
	
	public static void printRawSohuStock() throws IOException {

		Calendar cal = Calendar.getInstance();

		String dirName = "" + cal.get(Calendar.YEAR)
				+ (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);

		File f = new File("c:\\junmiaostock\\" + dirName);

		if (!f.exists()) {
			f.mkdir();
		}

		File f1 = new File("c:\\junmiaostock\\" + dirName + "\\raw");

		if (!f1.exists()) {
			f1.mkdir();
		}

		File f3 = new File("c:\\junmiaostock\\" + dirName + "\\raw"
				+ "\\shst.txt");

		FileWriter fw = new FileWriter(f3);
		BufferedWriter bw = new BufferedWriter(fw);

		ArrayList<SohuStockBean> ttgj = Execution.executeSohuStock();

		if (ttgj != null && ttgj.size() > 0) {
			for (int i = 0; i < ttgj.size(); i++) {
				SohuStockBean ggtj = ttgj.get(i);

				if (ggtj != null) {
					String line = ggtj.getStockNo() + ":" + ggtj.getStockName()
							+ ":" + ggtj.getBiao_xian_10_day() + ":"
							+ ggtj.getBiao_xian_5_day() + ":"
							+ ggtj.getHuan_shou_10_day() + ":"
							+ ggtj.getHuan_shou_5_day() + ":"
							+ ggtj.getJin_li_zen_zhang() + ":"
							+ ggtj.getJin_zi_zhan_shou_yi_lv() + ":"
							+ ggtj.getZhu_yin_zen_zhan_lv() + ":"
							+ ggtj.getLiu_tong_gu_ben() + ":"
							+ ggtj.getZong_gu_bin() + ":" 
							+ ggtj.getMei_gu_jin_zi_chan() + ":"
							+ ggtj.getMei_gu_shou_yi() + ":"
							+ ggtj.getMei_gu_wei_fen_pei() + ":"
							+ ggtj.getMei_gu_zi_ben_gong_ji_jin()+":"
							+ ggtj.getYearHigh()+ ":"
					        + ggtj.getYearLow();
					

					bw.write(line + "\n");
				}
			}
		}

		bw.close();
		fw.close();
	}
	
	public static void printRawSinaStock() throws IOException {

		Calendar cal = Calendar.getInstance();

		String dirName = "" + cal.get(Calendar.YEAR)
				+ (cal.get(Calendar.MONTH) + 1) + cal.get(Calendar.DATE);

		File f = new File("c:\\junmiaostock\\" + dirName);

		if (!f.exists()) {
			f.mkdir();
		}

		File f1 = new File("c:\\junmiaostock\\" + dirName + "\\raw");

		if (!f1.exists()) {
			f1.mkdir();
		}

		File f3 = new File("c:\\junmiaostock\\" + dirName + "\\raw"
				+ "\\sinaStock.txt");

		FileWriter fw = new FileWriter(f3);
		BufferedWriter bw = new BufferedWriter(fw);

		ArrayList<SinaStockBean> ttgj = Execution.executeSinaStock();

		if (ttgj != null && ttgj.size() > 0) {
			for (int i = 0; i < ttgj.size(); i++) {
				SinaStockBean ggtj = ttgj.get(i);

				if (ggtj != null) {
					String line = ggtj.getStockNo() + ":" + ggtj.getStockName();
					
					if(ggtj.getShareHolders()!=null&&ggtj.getShareHolders().size()==10)
					{
						for(int k=0;k<10;k++)
						{
							ShareHolder sh = ggtj.getShareHolders().get(k);
							if(sh!=null)
							{
								line += ":" + sh.getName()
									   + ":" + sh.getHolderAmount()
									   + ":" + sh.getOperation()
									   + ":" + sh.getDelta();
							}
						}
					}
					

					bw.write(line + "\n");
				}
			}
		}

		bw.close();
		fw.close();
	}
	
}
